package cn.egame.extraction.multithread.service.filter;

import java.util.List;

import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.extraction.multithread.pojo.EfsInfo;
import cn.egame.extraction.multithread.service.CrawlerThread;
import cn.egame.extraction.multithread.util.ConstVar_EFS;

public class CheckFileExistsFilter extends Filter {

	@Override
	public void handleRequest(ChainObject chainObject) throws ExceptionCommonBase {
		EfsInfo efsInfo = (EfsInfo) chainObject.get("efsInfo");
		CrawlerThread currentThread = (CrawlerThread) chainObject.get("currentThread");
		List<String> filePathList = (List<String>) chainObject.get("filePathList");
		int checkNum = Utils.toInt(chainObject.get("checkNum"), 0);
		if(efsInfo!=null
				&& currentThread!=null && filePathList!=null){
			if(checkNum==2){
				//拉取后检查文件是否存在
				boolean exists = currentThread.getFileService().isFilesExists(filePathList);
				if (exists) {
					currentThread.logger.info("id为" + efsInfo.getEfs_id() + "的文件拉取成功");
					currentThread.getFileService().fetchOkAndWriteBack(
							currentThread.getFilename(), efsInfo);
					return;
				}
			}else{
				currentThread.logger.info("准备拉取id为"+efsInfo.getEfs_id()+"的文件");
				
				//先判断该id对应的文件是否存在
				boolean exists = currentThread.getFileService().isFilesExists(filePathList);
				if (exists) {
					currentThread.logger.info("id为" + efsInfo.getEfs_id() + "的文件已存在");
					currentThread.getFileService().fetchOkAndWriteBack(
							currentThread.getFilename(), efsInfo);
					return;
				}else{
					getSuccessor().handleRequest(chainObject);
				}
			}
		}
		
	}



}
